package com.accesshollywood.grfxOrdering.Dialogs;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Window;
import java.awt.Dialog.ModalityType;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;

import com.accesshollywood.grfxOrdering.basics.Users;
import com.accesshollywood.indexables.User;

public class NewPersonDialog extends AbstractCustomDialog {
	private final JPanel	contentPanel	= new JPanel();
	private JTextField		txtFirstName;
	private JTextField		txtSSO;
	private JTextField		txtLastName;
	private JTextField		txtEmailAddress;
	private JCheckBox		chckbxPhotoEdit;
	private JCheckBox		chckbxProducer;
	private JCheckBox		chckbxArtist;
	private JCheckBox		chckbxAdmin;
	private JCheckBox		chckbxFootage;
	private JCheckBox		chckbxLegal;
	private JCheckBox		chckbxResearch;
	private JCheckBox		chckbxOrderer;
	private JCheckBox		chckbxGuest;
	public User				newUserPointer	= null;

	public NewPersonDialog(Window w, ModalityType modality) {
		super(w, modality);
		setModal(true);
		createDialog();
	}

	public NewPersonDialog(boolean createNow, Window w, ModalityType modality) {
		super(w, modality);
		setModal(true);
		if (createNow) {
			createDialog();
		}
	}

	/**
	 * Create the dialog.
	 */
	public void createDialog() {
		setBounds(100, 100, 470, 578);
		setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.Y_AXIS));

		if (Users.currentUser == null) {
			JEditorPane txtGreeting = new JEditorPane();
			txtGreeting.setMaximumSize(new Dimension(2147483647, 80));
			txtGreeting.setText("Hi,\nThe system does not recognize you.\n\nPlease enter your name and job level.");
			contentPanel.add(txtGreeting);
			Component rigidArea = Box.createRigidArea(new Dimension(20, 20));
			contentPanel.add(rigidArea);
		}
		{
			JPanel panel = new JPanel();
			contentPanel.add(panel);
			panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
			{
				JLabel lblNewLabel = new JLabel("First");
				panel.add(lblNewLabel);
			}
			{
				txtFirstName = new JTextField();
				txtFirstName.setMaximumSize(new Dimension(2147483647, 20));
				txtFirstName.setAlignmentX(Component.RIGHT_ALIGNMENT);
				panel.add(txtFirstName);
				txtFirstName.setColumns(10);
			}
			{
				Component rigidArea = Box.createRigidArea(new Dimension(20, 20));
				panel.add(rigidArea);
			}
			{
				JLabel lblLast = new JLabel("Last");
				panel.add(lblLast);
			}
			{
				txtLastName = new JTextField();
				txtLastName.setMaximumSize(new Dimension(2147483647, 20));
				panel.add(txtLastName);
				txtLastName.setColumns(10);
			}
			{
				Component rigidArea = Box.createRigidArea(new Dimension(20, 20));
				panel.add(rigidArea);
			}
			{
				JLabel lblEmail = new JLabel("Email");
				panel.add(lblEmail);
			}
			{
				txtEmailAddress = new JTextField();
				txtEmailAddress.setMaximumSize(new Dimension(2147483647, 20));
				panel.add(txtEmailAddress);
				txtEmailAddress.setColumns(10);
			}
		}
		{
			Component rigidArea = Box.createRigidArea(new Dimension(20, 20));
			contentPanel.add(rigidArea);
		}
		{
			JPanel panel = new JPanel();
			contentPanel.add(panel);
			panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
			{
				JLabel lblSso = new JLabel("SSO:");
				panel.add(lblSso);
			}
			{
				txtSSO = new JTextField();
				txtSSO.setMaximumSize(new Dimension(2147483647, 20));
				txtSSO.setAlignmentX(Component.RIGHT_ALIGNMENT);

				if (Users.currentUser == null) {
					txtSSO.setText(System.getProperty("user.name"));
				}
				panel.add(txtSSO);
				txtSSO.setColumns(10);
			}
		}
		{
			Component rigidArea = Box.createRigidArea(new Dimension(20, 20));
			contentPanel.add(rigidArea);
		}
		{
			JPanel panel = new JPanel();
			panel.setAlignmentX(Component.LEFT_ALIGNMENT);
			panel.setBorder(new TitledBorder(null, "Permissions", TitledBorder.LEADING, TitledBorder.TOP, null, null));
			contentPanel.add(panel);
			panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
			chckbxOrderer = new JCheckBox("Can Order");
			chckbxResearch = new JCheckBox("Can Research");
			chckbxPhotoEdit = new JCheckBox("PhotoEdit");
			chckbxFootage = new JCheckBox("Footage");
			chckbxLegal = new JCheckBox("Legal");
			chckbxArtist = new JCheckBox("Artist");
			chckbxProducer = new JCheckBox("Producer");
			chckbxAdmin = new JCheckBox("Admin");
			chckbxGuest = new JCheckBox("Guest");
			chckbxGuest.setSelected(true);

			// if (Users.currentUser != null) {
			panel.add(chckbxOrderer);
			panel.add(chckbxPhotoEdit);
			panel.add(chckbxFootage);
			panel.add(chckbxLegal);
			panel.add(chckbxArtist);
			panel.add(chckbxAdmin);
			// }
			panel.add(chckbxResearch);
			panel.add(chckbxProducer);
			panel.add(chckbxGuest);
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						OK();
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						cancel();
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}

		setLocationRelativeTo(null);

		this.setVisible(true);
	}

	public void OK() {
		newUserPointer = makePerson();
		this.dispose();
	}

	public void cancel() {
		newUserPointer = null;
		this.dispose();
	}

	public User makePerson() {
		User newUser = new User();
		newUser.setFirstName(txtFirstName.getText());
		newUser.setLastName(txtLastName.getText());
		newUser.setEmailAddress(txtEmailAddress.getText());
		newUser.setAdmin(chckbxAdmin.isSelected());
		newUser.setOrderer(chckbxOrderer.isSelected());
		newUser.setResearch(chckbxResearch.isSelected());
		newUser.setLegal(chckbxLegal.isSelected());
		newUser.setPhotoEdit(chckbxPhotoEdit.isSelected());
		newUser.setFootage(chckbxFootage.isSelected());
		newUser.setArtist(chckbxArtist.isSelected());
		newUser.setProducer(chckbxProducer.isSelected());
		newUser.setGuest(chckbxGuest.isSelected());
		newUser.setSSO(Integer.parseInt(txtSSO.getText()));

		Users.addUserToLists(newUser);
		Users.addUserToSQL(newUser);
		return newUser;
	}

	public JCheckBox getChckbxGuest() {
		return chckbxGuest;
	}
}
